

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <link rel="icon" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Ikki">
  <meta name="keywords" content="">
  
    <meta name="description" content="1. 布署软件的问题    如果想让软件运行起来要保证操作系统的设置，各种库和组件的安装都是正确的   热带鱼&amp;冷水鱼 冷水鱼适应的水温在5-30度，而热带鱼只能适应22-30度水温，低于22度半小时就冻死了  2. 虚拟机  虚拟机（virtual machine）就是带环境安装的一种解决方案。它可以在一种操作系统里面运行另一种操作系统   资源占用多   冗余步骤多   启动速度慢">
<meta property="og:type" content="article">
<meta property="og:title" content="01.docker基础">
<meta property="og:url" content="http://81.68.132.127/2022/10/24/dev_ops/docker/01.docker%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="IKKI">
<meta property="og:description" content="1. 布署软件的问题    如果想让软件运行起来要保证操作系统的设置，各种库和组件的安装都是正确的   热带鱼&amp;冷水鱼 冷水鱼适应的水温在5-30度，而热带鱼只能适应22-30度水温，低于22度半小时就冻死了  2. 虚拟机  虚拟机（virtual machine）就是带环境安装的一种解决方案。它可以在一种操作系统里面运行另一种操作系统   资源占用多   冗余步骤多   启动速度慢">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-j3m8y5.jpeg">
<meta property="article:published_time" content="2022-10-24T12:26:05.000Z">
<meta property="article:modified_time" content="2022-10-29T18:23:23.434Z">
<meta property="article:author" content="Ikki">
<meta property="article:tag" content="运维">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-j3m8y5.jpeg">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>01.docker基础 - IKKI</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/fluid/css/fluid-extention.css">
<link rel="stylesheet" href="/fluid/css/nihao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"81.68.132.127","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":3},"lazyload":{"enable":true,"loading_img":"https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 100vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Ikki-BUG制造者</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('http://image.ikki0603.cn') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="01.docker基础"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-24 20:26" pubdate>
          2022年10月24日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          19k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          162 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
        <div class="scroll-down-bar">
          <i class="iconfont icon-arrowdown"></i>
        </div>
      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">01.docker基础</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2022年10月30日 凌晨
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="1-布署软件的问题"><a href="#1-布署软件的问题" class="headerlink" title="1. 布署软件的问题 "></a>1. 布署软件的问题 </h2><ul>
<li>  如果想让软件运行起来要保证操作系统的设置，各种库和组件的安装都是正确的</li>
<li>  热带鱼&amp;冷水鱼 冷水鱼适应的水温在5-30度，而热带鱼只能适应22-30度水温，低于22度半小时就冻死了</li>
</ul>
<h2 id="2-虚拟机"><a href="#2-虚拟机" class="headerlink" title="2. 虚拟机 "></a>2. 虚拟机 </h2><ul>
<li>虚拟机（virtual machine）就是带环境安装的一种解决方案。它可以在一种操作系统里面运行另一种操作系统<ul>
<li>  资源占用多</li>
<li>  冗余步骤多</li>
<li>  启动速度慢</li>
</ul>
</li>
</ul>
<h2 id="3-Linux-容器"><a href="#3-Linux-容器" class="headerlink" title="3. Linux 容器 "></a>3. Linux 容器 </h2><ul>
<li>  由于虚拟机存在这些缺点，Linux 发展出了另一种虚拟化技术：Linux 容器（Linux Containers，缩写为 LXC）。</li>
<li>Linux 容器不是模拟一个完整的操作系统，而是对进程进行隔离。或者说，在正常进程的外面套了一个保护层。对于容器里面的进程来说，它接触到的各种资源都是虚拟的，从而实现与底层系统的隔离。<ul>
<li>  启动快</li>
<li>  资源占用少</li>
<li>  体积小</li>
</ul>
</li>
</ul>
<h2 id="4-Docker-是什么"><a href="#4-Docker-是什么" class="headerlink" title="4. Docker 是什么 "></a>4. Docker 是什么 </h2><ul>
<li>  Docker 属于 Linux 容器的一种封装，提供简单易用的容器使用接口。它是目前最流行的 Linux 容器解决方案。</li>
<li>  Docker 将应用程序与该程序的依赖，打包在一个文件里面。运行这个文件，就会生成一个虚拟容器。程序在这个虚拟容器里运行，就好像在真实的物理机上运行一样</li>
</ul>
<h2 id="5-docker和KVM"><a href="#5-docker和KVM" class="headerlink" title="5. docker和KVM "></a>5. docker和KVM </h2><ul>
<li>启动时间<ul>
<li>  Docker秒级启动</li>
<li>  KVM分钟级启动</li>
</ul>
</li>
<li>轻量级 容器镜像通常以M为单位，虚拟机以G为单位，容器资源占用小，要比虚拟要部署更快速<ul>
<li>  容器共享宿主机内核，系统级虚拟化，占用资源少，容器性能基本接近物理机</li>
<li>  虚拟机需要虚拟化一些设备，具有完整的OS,虚拟机开销大，因而降低性能，没有容器性能好</li>
</ul>
</li>
<li>安全性<ul>
<li>  由于共享宿主机内核，只是进程隔离，因此隔离性和稳定性不如虚拟机，容器具有一定权限访问宿主机内核，存在一下安全隐患</li>
</ul>
</li>
<li>使用要求<ul>
<li>  KVM基于硬件的完全虚拟化，需要硬件CPU虚拟化技术支持</li>
<li>  容器共享宿主机内核，可运行在主机的Linux的发行版，不用考虑CPU是否支持虚拟化技术</li>
</ul>
</li>
</ul>
<h2 id="6-docker应用场景"><a href="#6-docker应用场景" class="headerlink" title="6. docker应用场景 "></a>6. docker应用场景 </h2><ul>
<li>节省项目环境部署时间<ul>
<li>  单项目打包</li>
<li>  整套项目打包</li>
<li>  新开源技术</li>
</ul>
</li>
<li>  环境一致性</li>
<li>  持续集成</li>
<li>  微服务</li>
<li>  弹性伸缩</li>
</ul>
<h2 id="7-Docker-体系结构"><a href="#7-Docker-体系结构" class="headerlink" title="7. Docker 体系结构 "></a>7. Docker 体系结构 </h2><ul>
<li>  containerd 是一个守护进程，使用runc管理容器，向Docker Engine提供接口</li>
<li>  shim 只负责管理一个容器</li>
<li>  runC是一个轻量级工具，只用来运行容器</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/dockerarch.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h2 id="8-Docker内部组件"><a href="#8-Docker内部组件" class="headerlink" title="8. Docker内部组件 "></a>8. Docker内部组件 </h2><ul>
<li>  <code>namespaces</code> 命名空间，Linux内核提供的一种对进程资源隔离的机制，例如进程、网络、挂载等资源</li>
<li>  <code>cgroups</code> 控制组,linux内核提供的一种限制进程资源的机制，例如cpu 内存等资源</li>
<li>  <code>unonFS</code> 联合文件系统，支持将不同位置的目录挂载到同一虚拟文件系统，形成一种分层的模型</li>
</ul>
<h2 id="9-docker安装"><a href="#9-docker安装" class="headerlink" title="9. docker安装 "></a>9. docker安装 </h2><ul>
<li>  docker分为企业版(EE)和社区版(CE)</li>
<li>  <a target="_blank" rel="noopener" href="https://docs.docker.com/install/linux/docker-ce/centos/">docker-ce</a></li>
<li>  <a target="_blank" rel="noopener" href="https://hub.docker.com/">hub.docker</a></li>
</ul>
<h3 id="9-1-安装"><a href="#9-1-安装" class="headerlink" title="9.1 安装"></a>9.1 安装</h3><figure class="highlight vim"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-lqco3wlacl5vpi"></i><span>vim</span><div class="collapse show" id="collapse-lqco3wlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs vim">yum install -<span class="hljs-keyword">y</span> yum-utils   device-mapper-persistent-data   lvm2<br>yum-config-manager     --<span class="hljs-built_in">add</span>-repo     https://download.docker.<span class="hljs-keyword">com</span>/linux/centos/docker-<span class="hljs-keyword">ce</span>.repo<br>yum install docker-<span class="hljs-keyword">ce</span> docker-<span class="hljs-keyword">ce</span>-cli containerd.io -<span class="hljs-keyword">y</span><br><br></code></pre></td></tr></table></div></figure>

<h3 id="9-2-启动"><a href="#9-2-启动" class="headerlink" title="9.2 启动"></a>9.2 启动</h3><figure class="highlight crmsh"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-2j68ahlacl5vpi"></i><span>crmsh</span><div class="collapse show" id="collapse-2j68ahlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">systemctl <span class="hljs-literal">start</span> docker<br><br></code></pre></td></tr></table></div></figure>

<h3 id="9-3-查看docker版本"><a href="#9-3-查看docker版本" class="headerlink" title="9.3 查看docker版本"></a>9.3 查看docker版本</h3><figure class="highlight crystal"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-7iakn7lacl5vpi"></i><span>crystal</span><div class="collapse show" id="collapse-7iakn7lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs crystal"><span class="hljs-variable">$ </span>docker version<br><span class="hljs-variable">$ </span>docker info<br><br></code></pre></td></tr></table></div></figure>



<h3 id="9-4-卸载"><a href="#9-4-卸载" class="headerlink" title="9.4 卸载"></a>9.4 卸载</h3><figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ud08gslacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-ud08gslacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs routeros">docker <span class="hljs-built_in">info</span><br>yum <span class="hljs-built_in">remove</span> docker<br>rm -rf /var/lib/docker<br><br></code></pre></td></tr></table></div></figure>

<h2 id="10-Docker架构"><a href="#10-Docker架构" class="headerlink" title="10. Docker架构 "></a>10. Docker架构 </h2><p><img src="http://img.zhufengpeixun.cn/docker-arch.jpg" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h2 id="11-阿里云加速"><a href="#11-阿里云加速" class="headerlink" title="11.阿里云加速 "></a>11.阿里云加速 </h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://dev.aliyun.com/search.html">镜像仓库</a></li>
<li>  <a target="_blank" rel="noopener" href="https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors">镜像加速器</a></li>
</ul>
<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-wsqkxelacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-wsqkxelacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs awk">sudo mkdir -p <span class="hljs-regexp">/etc/</span>docker<br>sudo tee <span class="hljs-regexp">/etc/</span>docker/daemon.json &lt;&lt;-<span class="hljs-string">&#x27;EOF&#x27;</span><br>&#123;<br>  <span class="hljs-string">&quot;registry-mirrors&quot;</span>: [<span class="hljs-string">&quot;https://fwvjnv59.mirror.aliyuncs.com&quot;</span>]<br>&#125;<br>EOF<br><span class="hljs-comment"># 重载所有修改过的配置文件</span><br><span class="hljs-regexp">//</span>daemon-reload: 重新加载某个服务的配置文件<br>sudo systemctl daemon-reload<br>sudo systemctl restart docker<br><br></code></pre></td></tr></table></div></figure>

<h2 id="12-image镜像"><a href="#12-image镜像" class="headerlink" title="12. image镜像 "></a>12. image镜像 </h2><ul>
<li>  Docker 把应用程序及其依赖，打包在 image 文件里面。只有通过这个文件，才能生成 Docker 容器</li>
<li>  image 文件可以看作是容器的模板</li>
<li>  Docker 根据 image 文件生成容器的实例</li>
<li>  同一个 image 文件，可以生成多个同时运行的容器实例</li>
<li>  镜像不是一个单一的文件，而是有多层(例如可以在centos镜像上再加上redis镜像，生成一个新的镜像)</li>
<li>  容器其实就是在镜像的最上面加了一层读写层，在运行容器里做的任何文件改动，都会写到这个读写层里。如果容器删除了，最上面的读写层也就删除了，改动也就丢失了</li>
<li>  我们可以通过<code>docker history &lt;ID/NAME&gt;</code> 查看镜像中各层内容及大小，每层对应着<code>Dockerfile</code>中的一条指令</li>
</ul>
<table>
<thead>
<tr>
<th>命令</th>
<th>含义</th>
<th>语法</th>
<th>案例</th>
</tr>
</thead>
<tbody><tr>
<td>ls</td>
<td>查看全部镜像</td>
<td>docker image ls</td>
<td></td>
</tr>
<tr>
<td>search</td>
<td>查找镜像</td>
<td>docker search [imageName]</td>
<td></td>
</tr>
<tr>
<td>history</td>
<td>查看镜像历史</td>
<td>docker history [imageName]</td>
<td></td>
</tr>
<tr>
<td>inspect</td>
<td>显示一个或多个镜像详细信息</td>
<td>docker inspect [imageName]</td>
<td></td>
</tr>
<tr>
<td>pull</td>
<td>拉取镜像</td>
<td>docker pull [imageName]</td>
<td></td>
</tr>
<tr>
<td>push</td>
<td>推送一个镜像到镜像仓库</td>
<td>docker push [imageName]</td>
<td></td>
</tr>
<tr>
<td>rmi</td>
<td>删除镜像</td>
<td>docker rmi [imageName] docker image rmi 2</td>
<td></td>
</tr>
<tr>
<td>prune</td>
<td>移除未使用的镜像，没有标记或补任何容器引用</td>
<td>docker image prune</td>
<td>docker image prune</td>
</tr>
<tr>
<td>tag</td>
<td>标记本地镜像，将其归入某一仓库</td>
<td>docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]</td>
<td>docker tag centos:7 zhangrenyang/centos:v1</td>
</tr>
<tr>
<td>export</td>
<td>将容器文件系统作为一个tar归档文件导出到STDOUT</td>
<td>docker export [OPTIONS] CONTAINER</td>
<td>docker export -o hello-world.tar b2712f1067a3</td>
</tr>
<tr>
<td>import</td>
<td>导入容器快照文件系统tar归档文件并创建镜像</td>
<td>docker import [OPTIONS] file/URL/- [REPOSITORY[:TAG]]</td>
<td>docker import hello-world.tar</td>
</tr>
<tr>
<td>save</td>
<td>将指定镜像保存成<code>tar</code>文件</td>
<td>docker save [OPTIONS] IMAGE [IMAGE…]</td>
<td>docker save -o hello-world.tar hello-world:latest</td>
</tr>
<tr>
<td>load</td>
<td>加载tar文件并创建镜像</td>
<td></td>
<td>docker load -i hello-world.tar</td>
</tr>
<tr>
<td>build</td>
<td>根据Dockerfile构建镜像</td>
<td>docker build [OPTIONS] PATH / URL / -</td>
<td>docker build -t zf/ubuntu:v1 .</td>
</tr>
</tbody></table>
<h3 id="12-1-ls-查看镜像"><a href="#12-1-ls-查看镜像" class="headerlink" title="12.1 ls-查看镜像"></a>12.1 ls-查看镜像</h3><figure class="highlight mel"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-89pldilacl5vpi"></i><span>mel</span><div class="collapse show" id="collapse-89pldilacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs mel">docker <span class="hljs-keyword">image</span> <span class="hljs-keyword">ls</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-24-20-54-53_fb4b2745.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<table>
<thead>
<tr>
<th>字段</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>REPOSITORY</td>
<td>仓库地址</td>
</tr>
<tr>
<td>TAG</td>
<td>标签</td>
</tr>
<tr>
<td>IMAGE_ID</td>
<td>镜像ID</td>
</tr>
<tr>
<td>CREATED</td>
<td>创建时间</td>
</tr>
<tr>
<td>SIZE</td>
<td>镜像大小</td>
</tr>
</tbody></table>
<h3 id="12-2-search-查找镜像"><a href="#12-2-search-查找镜像" class="headerlink" title="12.2 search-查找镜像"></a>12.2 search-查找镜像</h3><figure class="highlight ebnf"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-1nul1wlacl5vpi"></i><span>ebnf</span><div class="collapse show" id="collapse-1nul1wlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">docker search ubuntu</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-24-20-55-45_df3f3979.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<table>
<thead>
<tr>
<th>字段</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>NAME</td>
<td>名称</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>描述</td>
</tr>
<tr>
<td>STARTS</td>
<td>星星的数量</td>
</tr>
<tr>
<td>OFFICIAL</td>
<td>是否官方源</td>
</tr>
</tbody></table>
<h3 id="12-3-pull-拉取镜像"><a href="#12-3-pull-拉取镜像" class="headerlink" title="12.3 pull-拉取镜像"></a>12.3 pull-拉取镜像</h3><figure class="highlight bash"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-7bfx32lacl5vpi"></i><span>bash</span><div class="collapse show" id="collapse-7bfx32lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker  pull docker.io/hello-world<br>docker image <span class="hljs-built_in">ls</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-24-20-59-42_905543e7.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>  docker image pull是抓取 image 文件的命令</li>
<li>  docker.io/hello-world是 image 文件在仓库里面的位置，其中<code>docker.io</code>是 image的作者，hello-world是 image 文件的名字</li>
<li>  Docker 官方提供的 image 文件，都放在<code>docker.io</code>组里面，所以它的是默认组，可以省略 <code>docker image pull hello-world</code></li>
</ul>
<h3 id="12-4-rmi-删除镜像"><a href="#12-4-rmi-删除镜像" class="headerlink" title="12.4 rmi-删除镜像"></a>12.4 rmi-删除镜像</h3><figure class="highlight ebnf"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-fwah71lacl5vpi"></i><span>ebnf</span><div class="collapse show" id="collapse-fwah71lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">docker rmi  hello-world</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-24-21-00-42_f56746c0.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="12-5-history-镜像历史"><a href="#12-5-history-镜像历史" class="headerlink" title="12.5 history-镜像历史"></a>12.5 history-镜像历史</h3><figure class="highlight bash"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-1u60rwlacl5vpi"></i><span>bash</span><div class="collapse show" id="collapse-1u60rwlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker <span class="hljs-built_in">history</span> centos<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-07-53-23_e7537c79.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="12-6-export-将容器文件系统作为一个tar归档文件导出到STDOUT"><a href="#12-6-export-将容器文件系统作为一个tar归档文件导出到STDOUT" class="headerlink" title="12.6 export-将容器文件系统作为一个tar归档文件导出到STDOUT"></a>12.6 export-将容器文件系统作为一个tar归档文件导出到STDOUT</h3><figure class="highlight stylus"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-5ejn8rlacl5vpi"></i><span>stylus</span><div class="collapse show" id="collapse-5ejn8rlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs stylus">docker export -o <span class="hljs-selector-attr">[文件名]</span><span class="hljs-selector-class">.tar</span> <span class="hljs-selector-attr">[containerId]</span> <br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-02-26_4aeb220d.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="12-7-import-导入容器快照文件系统tar归档文件并创建镜像"><a href="#12-7-import-导入容器快照文件系统tar归档文件并创建镜像" class="headerlink" title="12.7 import-导入容器快照文件系统tar归档文件并创建镜像"></a>12.7 import-导入容器快照文件系统tar归档文件并创建镜像</h3><figure class="highlight actionscript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-u8m294lacl5vpi"></i><span>actionscript</span><div class="collapse show" id="collapse-u8m294lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs actionscript">docker <span class="hljs-meta"><span class="hljs-keyword">import</span> [文件名].tar</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-05-18_5900bf31.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="12-7-save-将指定镜像保存成tar文件"><a href="#12-7-save-将指定镜像保存成tar文件" class="headerlink" title="12.7 save-将指定镜像保存成tar文件"></a>12.7 save-将指定镜像保存成tar文件</h3><figure class="highlight stylus"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-smctchlacl5vpi"></i><span>stylus</span><div class="collapse show" id="collapse-smctchlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs stylus">docker save -o <span class="hljs-selector-attr">[文件名]</span><span class="hljs-selector-class">.tar</span> <span class="hljs-selector-attr">[image name]</span>:<span class="hljs-selector-attr">[tag]</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-09-25_be94daff.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="12-8-load-加载tar文件并创建镜像"><a href="#12-8-load-加载tar文件并创建镜像" class="headerlink" title="12.8 load-加载tar文件并创建镜像"></a>12.8 load-加载tar文件并创建镜像</h3><figure class="highlight css"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-37uvk6lacl5vpi"></i><span>css</span><div class="collapse show" id="collapse-37uvk6lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs css">docker load -<span class="hljs-selector-tag">i</span> <span class="hljs-selector-attr">[文件名]</span><span class="hljs-selector-class">.tar</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-12-03_e3de1b50.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>  用户既可以使用 <code>docker load</code> 来导入镜像存储文件到本地镜像库，也可以使用 <code>docker import</code> 来导入一个容器快照到本地镜像库</li>
<li>  这两者的区别在于容器(import)快照文件将丢弃所有的历史记录和元数据信息（即仅保存容器当时的快照状态），而镜像(load)存储文件将保存完整记录，体积也要大</li>
<li>  此外，从容器(import)快照文件导入时可以重新指定标签等元数据信息</li>
</ul>
<h2 id="13-容器"><a href="#13-容器" class="headerlink" title="13. 容器 "></a>13. 容器 </h2><ul>
<li>  <code>docker run</code> 命令会从 <code>image</code> 文件，生成一个正在运行的容器实例。</li>
<li>  <code>docker container run</code>命令具有自动抓取 image 文件的功能。如果发现本地没有指定的 image 文件，就会从仓库自动抓取</li>
<li>  输出提示以后，hello world就会停止运行，容器自动终止。</li>
<li>  有些容器不会自动终止</li>
<li>  image 文件生成的容器实例，本身也是一个文件，称为容器文件</li>
<li>  容器生成，就会同时存在两个文件： image 文件和容器文件</li>
<li>  关闭容器并不会删除容器文件，只是容器停止运行</li>
</ul>
<h3 id="13-1-命令"><a href="#13-1-命令" class="headerlink" title="13.1 命令"></a>13.1 命令</h3><table>
<thead>
<tr>
<th>命令</th>
<th>含义</th>
<th>案例</th>
</tr>
</thead>
<tbody><tr>
<td>run</td>
<td>从镜像运行一个容器</td>
<td>docker run ubuntu /bin/echo ‘hello-world’</td>
</tr>
<tr>
<td>ls</td>
<td>列出容器</td>
<td>docker container ls</td>
</tr>
<tr>
<td>inspect</td>
<td>显示一个或多个容器详细信息</td>
<td>docker inspect</td>
</tr>
<tr>
<td>attach</td>
<td>要attach上去的容器必须正在运行，可以同时连接上同一个container来共享屏幕</td>
<td>docker attach [OPTIONS] CONTAINER</td>
</tr>
<tr>
<td>stats</td>
<td>显示容器资源使用统计</td>
<td>docker container stats</td>
</tr>
<tr>
<td>top</td>
<td>显示一个容器运行的进程</td>
<td>docker container top</td>
</tr>
<tr>
<td>update</td>
<td>更新一个或多个容器配置</td>
<td></td>
</tr>
<tr>
<td>port</td>
<td>列出指定的容器的端口映射</td>
<td>docker run -d -p 8080:80 nginx docker container port containerID</td>
</tr>
<tr>
<td>ps</td>
<td>查看当前运行的容器</td>
<td>docker ps -a -l</td>
</tr>
<tr>
<td>kill [containerId]</td>
<td>终止容器(发送SIGKILL )</td>
<td>docker kill [containerId]</td>
</tr>
<tr>
<td>rm [containerId]</td>
<td>删除容器</td>
<td>docker rm [containerId]</td>
</tr>
<tr>
<td>start [containerId]</td>
<td>启动已经生成、已经停止运行的容器文件</td>
<td>docker start [containerId]</td>
</tr>
<tr>
<td>stop [containerId]</td>
<td>终止容器运行 (发送 SIGTERM )</td>
<td>docker stop [containerId] docker container stop $(docker container ps -aq)</td>
</tr>
<tr>
<td>logs [containerId]</td>
<td>查看 docker 容器的输出</td>
<td>docker logs [containerId]</td>
</tr>
<tr>
<td>exec [containerId]</td>
<td>进入一个正在运行的 docker 容器执行命令</td>
<td>docker container exec -it f6a53629488b /bin/bash</td>
</tr>
<tr>
<td>cp [containerId]</td>
<td>从正在运行的 Docker 容器里面，将文件拷贝到本机</td>
<td>docker container cp f6a53629488b:/root/root.txt .</td>
</tr>
<tr>
<td>commit [containerId]</td>
<td>根据一个现有容器创建一个新的镜像</td>
<td>docker commit -a “zhufeng” -m “mynginx” a404c6c174a2 mynginx:v1</td>
</tr>
</tbody></table>
<ul>
<li>docker容器的主线程（dockfile中CMD执行的命令）结束，容器会退出<ul>
<li>  以使用交互式启动 <code>docker run -i [CONTAINER_NAME or CONTAINER_ID]</code></li>
<li>  tty选项 <code>docker run -dit [CONTAINER_NAME or CONTAINER_ID]</code></li>
<li>  守护态（Daemonized）形式运行 <code>docker run -d ubuntu /bin/sh -c &quot;while true; do echo hello world; sleep 1; done&quot;</code></li>
</ul>
</li>
</ul>
<h3 id="13-2-run-启动容器"><a href="#13-2-run-启动容器" class="headerlink" title="13.2 run-启动容器"></a>13.2 run-启动容器</h3><figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-nwdmxilacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-nwdmxilacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker run ubuntu <span class="hljs-regexp">/bin/</span>echo <span class="hljs-string">&quot;Hello world&quot;</span><br></code></pre></td></tr></table></div></figure>

<ul>
<li>  docker: Docker 的二进制执行文件。</li>
<li>  run:与前面的 docker 组合来运行一个容器。</li>
<li>  ubuntu指定要运行的镜像，Docker首先从本地主机上查找镜像是否存在，如果不存在，Docker 就会从镜像仓库 Docker Hub 下载公共镜像。</li>
<li>  /bin/echo “Hello world”: 在启动的容器里执行的命令</li>
</ul>
<blockquote>
<p>Docker以ubuntu镜像创建一个新容器，然后在容器里执行 bin/echo “Hello world”，然后输出结果</p>
<ul>
<li>  Docker attach必须是登陆到一个已经运行的容器里。需要注意的是如果从这个容器中exit退出的话，就会导致容器停止</li>
</ul>
</blockquote>
<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-i –interactive=false</td>
<td>交互式</td>
</tr>
<tr>
<td>-t –tty</td>
<td>分配一个伪终端</td>
</tr>
<tr>
<td>-d –detach=false</td>
<td>运行容器到后台</td>
</tr>
<tr>
<td>-a –attach=[]</td>
<td>附加到运行的容器</td>
</tr>
<tr>
<td>-e –env=[]</td>
<td>设置环境变量</td>
</tr>
<tr>
<td>-p –publish=[]</td>
<td>发布容器端口到主机</td>
</tr>
<tr>
<td>-P -publish-all=false</td>
<td>自动映射容器对外提供服务的端口</td>
</tr>
<tr>
<td>-c, –cpu-shares=0</td>
<td>设置cpu使用权重</td>
</tr>
<tr>
<td>–cidfile=””</td>
<td>把容器id写入到指定文件</td>
</tr>
<tr>
<td>–cpuset=””</td>
<td>cpu绑定</td>
</tr>
<tr>
<td>–dns=[]</td>
<td>设置dns</td>
</tr>
<tr>
<td>–dns-search=[]</td>
<td>设置dns域搜索</td>
</tr>
<tr>
<td>-e, –env=[]</td>
<td>定义环境变量</td>
</tr>
<tr>
<td>–env-file=[]</td>
<td>从指定文件读取变量值</td>
</tr>
<tr>
<td>–expose=[]</td>
<td>指定对外提供服务端口</td>
</tr>
<tr>
<td>-h, –hostname=””</td>
<td>设置容器主机名</td>
</tr>
<tr>
<td>–link=[]</td>
<td>添加链接到另外一个容器</td>
</tr>
<tr>
<td>–lxc-conf=[]</td>
<td></td>
</tr>
<tr>
<td>-m, –memory=””</td>
<td>内存限制</td>
</tr>
<tr>
<td>–name=””</td>
<td>设置容器名</td>
</tr>
<tr>
<td>–net=”bridge”</td>
<td>设置容器网络模式(bridge,none,container:&lt;name</td>
</tr>
<tr>
<td>–privileged=false</td>
<td>提供更多的权限给容器</td>
</tr>
<tr>
<td>–rm=false</td>
<td>如果容器退出自动移除和-d选项冲突</td>
</tr>
<tr>
<td>-u, –user=””</td>
<td>指定运行容器的用户uid或者用户名</td>
</tr>
<tr>
<td>-v, –volume=[]</td>
<td>挂载卷</td>
</tr>
<tr>
<td>–volumes-from=[]</td>
<td>从指定容器挂载卷</td>
</tr>
<tr>
<td>-w, –workdir=””</td>
<td>指定容器工作目录</td>
</tr>
</tbody></table>
<h3 id="13-3-ps-查看容器"><a href="#13-3-ps-查看容器" class="headerlink" title="13.3 ps-查看容器"></a>13.3 ps-查看容器</h3><figure class="highlight css"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-tfshevlacl5vpi"></i><span>css</span><div class="collapse show" id="collapse-tfshevlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs css">docker ps<br>docker -<span class="hljs-selector-tag">a</span><br>docker -l<br></code></pre></td></tr></table></div></figure>

<ul>
<li>  -a 显示所有的容器，包括已停止的</li>
<li>  -l 显示最新的那个容器</li>
<li>  -q 只显示容器的id</li>
</ul>
<table>
<thead>
<tr>
<th>字段</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>CONTAINER ID</td>
<td>容器ID</td>
</tr>
<tr>
<td>IMAGE</td>
<td>使用的镜像</td>
</tr>
<tr>
<td>COMMAND</td>
<td>使用的命令</td>
</tr>
<tr>
<td>CREATED</td>
<td>创建时间</td>
</tr>
<tr>
<td>STATUS</td>
<td>状态</td>
</tr>
<tr>
<td>PORTS</td>
<td>端口号</td>
</tr>
<tr>
<td>NAMES</td>
<td>自动分配的名称</td>
</tr>
</tbody></table>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-06-36_efa437af.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-26-25_9afaa96c.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-4-it-运行交互式的容器"><a href="#13-4-it-运行交互式的容器" class="headerlink" title="13.4 -it-运行交互式的容器"></a>13.4 -it-运行交互式的容器</h3><figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-bhw10ylacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-bhw10ylacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker run -i -t ubuntu <span class="hljs-regexp">/bin/</span>bash<br></code></pre></td></tr></table></div></figure>

<ul>
<li>  -t=–interactive 在新容器内指定一个伪终端或终端。</li>
<li>  -i=–tty 允许你对容器内的标准输入 (STDIN) 进行交互。</li>
</ul>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-10-41_2dc9a2fb.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<blockquote>
<p>我们可以通过运行exit命令或者使用CTRL+D来退出容器。</p>
</blockquote>
<h3 id="13-5-d-后台运行容器"><a href="#13-5-d-后台运行容器" class="headerlink" title="13.5 -d-后台运行容器"></a>13.5 -d-后台运行容器</h3><figure class="highlight stylus"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-i2snhjlacl5vpi"></i><span>stylus</span><div class="collapse show" id="collapse-i2snhjlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs stylus">docker run -d centos ping www<span class="hljs-selector-class">.baidu</span><span class="hljs-selector-class">.com</span><br>docker ps <br>docker logs <span class="hljs-attr">--follow</span> <span class="hljs-number">5410</span>a9dc4218 <span class="hljs-comment">//查看容器后台运行的输出日志</span><br>docker stop <span class="hljs-number">5410</span>a9dc4218 <span class="hljs-comment">//终止容器运行</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-15-35_e8153c97.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-6-kill-强制终止容器运行"><a href="#13-6-kill-强制终止容器运行" class="headerlink" title="13.6 kill-强制终止容器运行"></a>13.6 kill-强制终止容器运行</h3><figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-dmgx2mlacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-dmgx2mlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> kill <span class="hljs-number">5</span>a5c3a760f61<br></code></pre></td></tr></table></div></figure>

<blockquote>
<p>kill是不管容器同不同意，直接执行<code>kill -9</code>，强行终止；</p>
</blockquote>
<blockquote>
<p>stop的话，首先给容器发送一个<code>TERM</code>信号，让容器做一些退出前必须的保护性、安全性操作，然后让容器自动停止运行，如果在一段时间内，容器还是没有停止，再进行kill -9，强行终止</p>
</blockquote>
<h3 id="13-7-rm-删除容器"><a href="#13-7-rm-删除容器" class="headerlink" title="13.7 rm-删除容器"></a>13.7 rm-删除容器</h3><figure class="highlight reasonml"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-yy8fezlacl5vpi"></i><span>reasonml</span><div class="collapse show" id="collapse-yy8fezlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs reasonml">docker rm 删除容器<br>docker rmi  删除镜像<br>docker rm <span class="hljs-constructor">$(<span class="hljs-params">docker</span> <span class="hljs-params">ps</span> -<span class="hljs-params">a</span> -<span class="hljs-params">q</span>)</span> <span class="hljs-comment">//批量删除</span><br></code></pre></td></tr></table></div></figure>
<h3 id="13-8-start-重新启动容器"><a href="#13-8-start-重新启动容器" class="headerlink" title="13.8 start-重新启动容器"></a>13.8 start-重新启动容器</h3><figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-e58pkklacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-e58pkklacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> start<span class="hljs-meta"> [containerId]</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-27-50_2c6c3650.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-9-stop-停止容器"><a href="#13-9-stop-停止容器" class="headerlink" title="13.9 stop-停止容器"></a>13.9 stop-停止容器</h3><figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-tucq2hlacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-tucq2hlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> stop<span class="hljs-meta"> [containerId]</span><br></code></pre></td></tr></table></div></figure>

<h3 id="13-10-attach-进入一个容器内部"><a href="#13-10-attach-进入一个容器内部" class="headerlink" title="13.10 attach-进入一个容器内部"></a>13.10 attach-进入一个容器内部</h3><figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-khf0j1lacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-khf0j1lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> attach<span class="hljs-meta"> [containerID]</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-29-05_a1bf7c09.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-11-exec-进入一个正在运行中的容器"><a href="#13-11-exec-进入一个正在运行中的容器" class="headerlink" title="13.11 exec-进入一个正在运行中的容器"></a>13.11 exec-进入一个正在运行中的容器</h3><figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-d20f1rlacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-d20f1rlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker container  exec -it [containerID] <span class="hljs-regexp">/bin/</span>bash<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-36-39_3a77557a.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-13-cp-拷贝文件"><a href="#13-13-cp-拷贝文件" class="headerlink" title="13.13 cp-拷贝文件"></a>13.13 cp-拷贝文件</h3><figure class="highlight bash"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-2l15ynlacl5vpi"></i><span>bash</span><div class="collapse show" id="collapse-2l15ynlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker container <span class="hljs-built_in">cp</span> [containerID]:/readme.md .<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-30-25_59d9a2eb.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-14-–rm-当容器停止的时候自动删除"><a href="#13-14-–rm-当容器停止的时候自动删除" class="headerlink" title="13.14 –rm-当容器停止的时候自动删除"></a>13.14 –rm-当容器停止的时候自动删除</h3><figure class="highlight dockerfile"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-tgzhsplacl5vpi"></i><span>dockerfile</span><div class="collapse show" id="collapse-tgzhsplacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile">docker <span class="hljs-keyword">run</span><span class="language-bash"> --<span class="hljs-built_in">rm</span> ubuntu /bin/bash</span><br></code></pre></td></tr></table></div></figure>

<h3 id="13-15-stats-容器资源使用统计"><a href="#13-15-stats-容器资源使用统计" class="headerlink" title="13.15 stats-容器资源使用统计"></a>13.15 stats-容器资源使用统计</h3><ul>
<li>  显示容器资源使用统计</li>
</ul>
<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-su9c2blacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-su9c2blacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker container stats <span class="hljs-regexp">//</span>全部容器的资源使用统计<br>docker container stats [containerID] <span class="hljs-regexp">//</span>当前容器资源使用统计<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-50-29_40fcee34.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-16-top-容器运行的进程"><a href="#13-16-top-容器运行的进程" class="headerlink" title="13.16 top-容器运行的进程"></a>13.16 top-容器运行的进程</h3><ul>
<li>  显示一个容器运行的进程</li>
</ul>
<figure class="highlight css"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-5ehkj9lacl5vpi"></i><span>css</span><div class="collapse show" id="collapse-5ehkj9lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs css">docker container <span class="hljs-attribute">top</span> <span class="hljs-selector-attr">[containerID]</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-20-14_ad4e581c.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-17-update-更新容器配置"><a href="#13-17-update-更新容器配置" class="headerlink" title="13.17 update-更新容器配置"></a>13.17 update-更新容器配置</h3><ul>
<li>  更新一个或多个容器配置</li>
</ul>
<figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-f04l7ylacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-f04l7ylacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> update -m <span class="hljs-number">500</span>m --memory-swap -<span class="hljs-number">1</span> <span class="hljs-meta"> [containerID]</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-24-10_f368c4bc.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-24-22_66f7b6e3.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-18-port"><a href="#13-18-port" class="headerlink" title="13.18 port"></a>13.18 port</h3><ul>
<li>  列出指定的容器的端口映射</li>
</ul>
<figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-rcoh1nlacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-rcoh1nlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> run -d -p <span class="hljs-number">8080</span>:<span class="hljs-number">80</span> nginx //nginx启动的<span class="hljs-number">80</span>端口映射到宿主的<span class="hljs-number">8080</span>端口上<br><span class="hljs-attribute">docker</span> container port <br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-16-53-04_9155dbaa.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-7dxr91lacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-7dxr91lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker run -d -P nginx <span class="hljs-regexp">//</span>nginx启动的<span class="hljs-number">80</span>端口映射到宿主的<span class="hljs-number">8</span>随机端口上<br>curl http:<span class="hljs-regexp">//</span>localhost:<span class="hljs-number">49153</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-08-42-48_244ff036.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="13-19-logs"><a href="#13-19-logs" class="headerlink" title="13.19 logs"></a>13.19 logs</h3><ul>
<li>  查看 docker 容器的输出<figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-1rxi1mlacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-1rxi1mlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> logs<span class="hljs-meta"> [containerId]</span><br></code></pre></td></tr></table></div></figure>
<img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-16-55-52_c53d7ffa.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<h3 id="13-20-create"><a href="#13-20-create" class="headerlink" title="13.20 create"></a>13.20 create</h3><ul>
<li>  创建一个容器但是不启动<figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-g2qtfclacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-g2qtfclacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> create<span class="hljs-meta"> [imageName]</span><br></code></pre></td></tr></table></div></figure>
<img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-29-08-12-02_04b554e2.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-29-08-12-02"></li>
</ul>
<h2 id="14-commit制作个性化镜像"><a href="#14-commit制作个性化镜像" class="headerlink" title="14. commit制作个性化镜像 "></a>14. commit制作个性化镜像 </h2><ul>
<li>  docker commit :从容器创建一个新的镜像。</li>
<li>docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]<ul>
<li>  -a :提交的镜像作者</li>
<li>  -c :使用Dockerfile指令来创建镜像</li>
<li>  -m :提交时的说明文字</li>
<li>  -p :在commit时，将容器暂停</li>
</ul>
</li>
<li>  停止容器后不会自动删除这个容器，除非在启动容器的时候指定了 –rm 标志</li>
<li>  使用 docker ps -a 命令查看 Docker 主机上包含停止的容器在内的所有容器</li>
<li>  停止状态的容器的可写层仍然占用磁盘空间。要清理可以使用 <code>docker container prune</code> 命令</li>
</ul>
<figure class="highlight mel"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-5pak5flacl5vpi"></i><span>mel</span><div class="collapse show" id="collapse-5pak5flacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs mel">docker <span class="hljs-keyword">container</span> commit -m <span class="hljs-string">&quot;Ikki nginx&quot;</span> -a <span class="hljs-string">&quot;ikki&quot;</span> f2cbb1568b81 ikki/ikki-nginx<br>docker <span class="hljs-keyword">image</span> <span class="hljs-keyword">ls</span><br>docker run -d -P ikki/ikki-nginx<br>docker <span class="hljs-keyword">container</span> rm <span class="hljs-number">6</span>fc104cec635<br>docker <span class="hljs-keyword">container</span> prune  <span class="hljs-comment">//移除所有停止的容器</span><br>docker <span class="hljs-keyword">image</span> rmi <span class="hljs-number">6</span>fc104cec635<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-09-15-48_8bcba3e0.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-09-17-25_316b7d58.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h2 id="15-制作Dockerfile"><a href="#15-制作Dockerfile" class="headerlink" title="15. 制作Dockerfile "></a>15. 制作Dockerfile </h2><ul>
<li>  Docker 的镜像是用一层一层的文件组成的</li>
<li>  <code>docker inspect</code>命令可以查看镜像或者容器</li>
<li>  Layers就是镜像的层文件，只读不能修改。基于镜像创建的容器会共享这些文件层</li>
</ul>
<figure class="highlight ebnf"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-006q56lacl5vpi"></i><span>ebnf</span><div class="collapse show" id="collapse-006q56lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">docker inspect centos</span><br></code></pre></td></tr></table></div></figure>

<h3 id="15-1-编写Dockerfile"><a href="#15-1-编写Dockerfile" class="headerlink" title="15.1 编写Dockerfile"></a>15.1 编写Dockerfile</h3><ul>
<li>  -t –tag list 镜像名称</li>
<li>  -f –file string 指定Dockerfile文件的位置</li>
</ul>
<table>
<thead>
<tr>
<th>指令</th>
<th>含义</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>FROM</td>
<td>构建的新镜像是基于哪个镜像</td>
<td>FROM centos:6</td>
</tr>
<tr>
<td>MAINTAINER</td>
<td>镜像维护者姓名或邮箱地址</td>
<td>MAINTAINER zhufengjiagou</td>
</tr>
<tr>
<td>RUN</td>
<td>构建镜像时运行的shell命令</td>
<td>RUN yum install httpd</td>
</tr>
<tr>
<td>CMD</td>
<td>CMD 设置容器启动后默认执行的命令及其参数，但 CMD 能够被 docker run 后面跟的命令行参数替换</td>
<td>CMD /usr/sbin/sshd -D</td>
</tr>
<tr>
<td>EXPOSE</td>
<td>声明容器运行的服务器端口</td>
<td>EXPOSE 80 443</td>
</tr>
<tr>
<td>ENV</td>
<td>设置容器内的环境变量</td>
<td>ENV MYSQL_ROOT_PASSWORD 123456</td>
</tr>
<tr>
<td>ADD</td>
<td>拷贝文件或目录到镜像中，如果是URL或者压缩包会自动下载和解压</td>
<td>ADD ,ADD <a target="_blank" rel="noopener" href="https://xxx.com/html.tar.gz">https://xxx.com/html.tar.gz</a> /var/<a target="_blank" rel="noopener" href="http://www.html/">www.html</a>, ADD html.tar.gz /var/www/html</td>
</tr>
<tr>
<td>COPY</td>
<td>拷贝文件或目录到镜像</td>
<td>COPY ./start.sh /start.sh</td>
</tr>
<tr>
<td>ENTRYPOINT</td>
<td>配置容器启动时运行的命令</td>
<td>ENTRYPOINT /bin/bash -c ‘/start.sh’</td>
</tr>
<tr>
<td>VOLUME</td>
<td>指定容器挂载点到宿主自动生成的目录或其它容器</td>
<td>VOLUME [“/var/lib/mysql”]</td>
</tr>
<tr>
<td>USER</td>
<td>为 RUN CMD和ENTRYPOINT执行命令指定运行用户</td>
<td>USER zhufengjiagou</td>
</tr>
<tr>
<td>WORKDIR</td>
<td>为RUN CMD ENTRYPOINT COPY ADD 设置工作目录</td>
<td>WORKDIR /data</td>
</tr>
<tr>
<td>HEALTHCHECK</td>
<td>健康检查</td>
<td>HEALTHCHECK –interval=5m –timeout=3s –retries=3 CMS curl -f htp://localhost</td>
</tr>
<tr>
<td>ARG</td>
<td>在构建镜像时指定一些参数</td>
<td>ARG user</td>
</tr>
</tbody></table>
<ul>
<li>  cmd给出的是一个容器的默认的可执行体。也就是容器启动以后，默认的执行的命令。重点就是这个”默认”。意味着，如果<code>docker run</code>没有指定任何的执行命令或者<code>dockerfile</code>里面也没有<code>entrypoint</code>，那么，就会使用cmd指定的默认的执行命令执行。同时也从侧面说明了<code>entrypoint</code>的含义，它才是真正的容器启动以后要执行命令</li>
</ul>
<h3 id="15-2-创建Dockerfile和-dockerignore"><a href="#15-2-创建Dockerfile和-dockerignore" class="headerlink" title="15.2 创建Dockerfile和.dockerignore"></a>15.2 创建Dockerfile和.dockerignore</h3><p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-50-06_4df2db45.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h4 id="15-2-1-dockerignore"><a href="#15-2-1-dockerignore" class="headerlink" title="15.2.1 .dockerignore"></a>15.2.1 .dockerignore</h4><p>表示要排除，不要打包到image中的文件路径</p>
<figure class="highlight asciidoc"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-weowy5lacl5vpi"></i><span>asciidoc</span><div class="collapse show" id="collapse-weowy5lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs asciidoc"><span class="hljs-title">.git</span><br>node_modules<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-17-50-42_17c53467.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h4 id="15-2-3-Dockerfile"><a href="#15-2-3-Dockerfile" class="headerlink" title="15.2.3 Dockerfile"></a>15.2.3 Dockerfile</h4><h5 id="15-2-3-1-宿主机安装node"><a href="#15-2-3-1-宿主机安装node" class="headerlink" title="15.2.3.1 宿主机安装node"></a>15.2.3.1 宿主机安装node</h5><ul>
<li>  <a target="_blank" rel="noopener" href="https://github.com/creationix/nvm/blob/master/README.md">nvm</a></li>
<li>如果安装node失败，可以查看这个教程：<a target="_blank" rel="noopener" href="https://blog.51cto.com/u_13760351/2382464">https://blog.51cto.com/u_13760351/2382464</a>  <figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-nleh8vlacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-nleh8vlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs awk">wget -qO- https:<span class="hljs-regexp">//</span>raw.githubusercontent.com<span class="hljs-regexp">/creationix/</span>nvm<span class="hljs-regexp">/v0.33.11/i</span>nstall.sh | bash<br>source <span class="hljs-regexp">/root/</span>.bashrc<br>nvm install stable<br>node -v<br>npm config set registry https:<span class="hljs-regexp">//</span>registry.npm.taobao.org<br>npm i cnpm -g  --registry https:<span class="hljs-regexp">//</span>registry.npm.taobao.org<br>npm i nrm -g  --registry https:<span class="hljs-regexp">//</span>registry.npm.taobao.org<br></code></pre></td></tr></table></div></figure></li>
</ul>
<h5 id="15-2-3-2-安装express项目生成器"><a href="#15-2-3-2-安装express项目生成器" class="headerlink" title="15.2.3.2 安装express项目生成器"></a>15.2.3.2 安装express项目生成器</h5><figure class="highlight maxima"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-xr3t3dlacl5vpi"></i><span>maxima</span><div class="collapse show" id="collapse-xr3t3dlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs maxima">npm install <span class="hljs-built_in">express</span>-generator -g<br>cd ikkiNode<br><span class="hljs-built_in">express</span> nodeproject //创建<span class="hljs-built_in">express</span>项目nodeproject<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-27-15_22897dfb.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h5 id="15-2-3-3-编写Dockerfile文件"><a href="#15-2-3-3-编写Dockerfile文件" class="headerlink" title="15.2.3.3 编写Dockerfile文件"></a>15.2.3.3 编写Dockerfile文件</h5><figure class="highlight dockerfile"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-lzy2rplacl5vpi"></i><span>dockerfile</span><div class="collapse show" id="collapse-lzy2rplacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile"><span class="hljs-comment"># 继承自node镜像</span><br><span class="hljs-keyword">FROM</span> node  <br><span class="hljs-comment"># 镜像维护者姓名或邮箱地址</span><br><span class="hljs-keyword">MAINTAINER</span> Ikki<br><span class="hljs-comment"># 拷贝当前项目文件(./nodeproject)或目录到镜像(/nodeproject)</span><br><span class="hljs-keyword">COPY</span><span class="language-bash"> ./nodeproject /nodeproject</span><br><span class="hljs-comment"># 为RUN CMD ENTRYPOINT COPY ADD 设置工作目录</span><br><span class="hljs-keyword">WORKDIR</span><span class="language-bash"> /nodeproject</span><br><span class="hljs-comment"># 为 RUN CMD和ENTRYPOINT执行命令指定运行用户</span><br><span class="hljs-keyword">USER</span> root<br><br><span class="hljs-comment"># 构建镜像时运行的shell命令</span><br><span class="hljs-keyword">RUN</span><span class="language-bash"> npm install</span><br><span class="hljs-comment"># 声明容器运行的服务器端口</span><br><span class="hljs-keyword">EXPOSE</span> <span class="hljs-number">3000</span><br><span class="hljs-comment"># 设置容器内的环境变量</span><br><span class="hljs-keyword">ENV</span> MYSQL_ROOT_PASSWORD <span class="hljs-number">123456</span><br><span class="hljs-comment"># CMD 设置容器启动后默认执行的命令及其参数，但 CMD 能够被 docker run 后面跟的命令行参数替换</span><br><span class="hljs-comment"># 如果设置了USER，那么CMD启动是默认用户就是USER的root</span><br><span class="hljs-keyword">CMD</span><span class="language-bash"> npm start</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-02-05_63f37c73.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>RUN命令在 image 文件的构建阶段执行，执行结果都会打包进入 image 文件；</li>
<li>CMD命令则是在容器启动后执行<blockquote>
<p>一个 Dockerfile 可以包含多个RUN命令，但是只能有一个CMD命令<br>指定了CMD命令以后，docker container run命令就不能附加命令了（比如前面的/bin/bash），否则它会覆盖CMD命令</p>
</blockquote>
<h3 id="15-3-创建image"><a href="#15-3-创建image" class="headerlink" title="15.3 创建image"></a>15.3 创建image</h3></li>
</ul>
<figure class="highlight maxima"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-gtg0m8lacl5vpi"></i><span>maxima</span><div class="collapse show" id="collapse-gtg0m8lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs maxima">docker build -t <span class="hljs-built_in">express</span>-<span class="hljs-built_in">demo</span> .<br>docker <span class="hljs-built_in">image</span> ls<br></code></pre></td></tr></table></div></figure>
<ul>
<li>  -t用来指定image镜像的名称，后面还可以加冒号指定标签，如果不指定默认就是latest</li>
<li>  <code>.</code> 表示Dockerfile文件的所有路径,<code>.</code>就表示当前路径</li>
</ul>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-37-03_d3c92871.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-37-32_d74462d0.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="15-4-使用新的镜像运行容器"><a href="#15-4-使用新的镜像运行容器" class="headerlink" title="15.4 使用新的镜像运行容器"></a>15.4 使用新的镜像运行容器</h3><figure class="highlight apache"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-wx3zwtlacl5vpi"></i><span>apache</span><div class="collapse show" id="collapse-wx3zwtlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> container run -p <span class="hljs-number">3333</span>:<span class="hljs-number">3000</span> -it express-demo:<span class="hljs-number">1</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span> /bin/bash<br><span class="hljs-attribute">npm</span> start<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-40-15_81f3839a.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-18-40-43_375fe126.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>  <code>-p</code> 参数是将容器的3000端口映射为本机的3333端口</li>
<li>  <code>-it</code> 参数是将容器的shell容器映射为当前的shell,在本机容器中执行的命令都会发送到容器当中执行</li>
<li>  <code>express-demo</code> image的名称</li>
<li>  /bin/bash 容器启动后执行的第一个命令,这里是启动了bash容器以便执行脚本</li>
<li>  <code>--rm</code> 在容器终止运行后自动删除容器文件</li>
</ul>
<h3 id="15-5-发布镜像image"><a href="#15-5-发布镜像image" class="headerlink" title="15.5 发布镜像image"></a>15.5 发布镜像image</h3><ul>
<li>  <a target="_blank" rel="noopener" href="https://hub.docker.com/">注册账户</a></li>
</ul>
<ul>
<li>根据dockerFile构建镜像<ul>
<li>  docker image build -t [username]/[repository]:[tag] .   </li>
</ul>
</li>
<li>将镜像打上标签<ul>
<li>  docker image tag [imageName] [username]/[repository]:[tag]   </li>
</ul>
</li>
<li>上传镜像<ul>
<li>  docker push [username]/[repository]:[tag]<figure class="highlight crmsh"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-66glbilacl5vpi"></i><span>crmsh</span><div class="collapse show" id="collapse-66glbilacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">docker login<br>docker image <span class="hljs-keyword">tag</span> <span class="hljs-title">hello-world</span> ikki6666/hello-world:latest<br>docker push ikki6666/hello-world:latest<br></code></pre></td></tr></table></div></figure>
<img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-22-04-17_c29dc2c0.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
</li>
</ul>
<h2 id="16-数据盘"><a href="#16-数据盘" class="headerlink" title="16. 数据盘 "></a>16. 数据盘 </h2><ul>
<li>  删除容器的时候，容器层里创建的文件也会被删除掉，如果有些数据你想永久保存，比如Web服务器的日志，数据库管理系统中的数据，可以为容器创建一个数据盘</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/bindmount.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="16-1-volume"><a href="#16-1-volume" class="headerlink" title="16.1 volume"></a>16.1 volume</h3><ul>
<li>  volumes Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)</li>
<li>  如果没有指定卷，则会自动创建</li>
<li>  建议使用–mount ,更通用</li>
</ul>
<table>
<thead>
<tr>
<th>命令</th>
<th>含义</th>
<th>语法</th>
</tr>
</thead>
<tbody><tr>
<td>create</td>
<td>创建数据卷</td>
<td>docker volume create [volumeName]</td>
</tr>
<tr>
<td>inspect</td>
<td>显示一个或多个卷的详细信息</td>
<td>docker volume inspect [volumeName]</td>
</tr>
<tr>
<td>ls</td>
<td>列出所有数据卷</td>
<td>docker volume ls</td>
</tr>
<tr>
<td>prune</td>
<td>删除所有未使用的本地数据卷</td>
<td>docker volume prune</td>
</tr>
<tr>
<td>rm</td>
<td>删除一个或多个卷</td>
<td>docker volume rm [volumeName]</td>
</tr>
</tbody></table>
<h3 id="16-1-1-创建数据卷"><a href="#16-1-1-创建数据卷" class="headerlink" title="16.1.1 创建数据卷"></a>16.1.1 创建数据卷</h3><figure class="highlight dockerfile"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-svzslklacl5vpi"></i><span>dockerfile</span><div class="collapse show" id="collapse-svzslklacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile">docker <span class="hljs-keyword">volume</span><span class="language-bash"> --<span class="hljs-built_in">help</span></span><br>docker <span class="hljs-keyword">volume</span><span class="language-bash"> create nginx-vol</span><br>docker <span class="hljs-keyword">volume</span><span class="language-bash"> <span class="hljs-built_in">ls</span></span><br>docker <span class="hljs-keyword">volume</span><span class="language-bash"> inspect nginx-vol</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-22-21-51_25f765d3.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>CreatedAt: 数据卷创建时间</li>
<li>Driver: 数据卷驱动,</li>
<li>Labels: 标签名,</li>
<li>Mountpoint: 挂载点,</li>
<li>Name: 名称,</li>
<li>Options: 操作,</li>
<li>Scope: 作用域</li>
</ul>
<h3 id="16-1-2-挂载数据卷"><a href="#16-1-2-挂载数据卷" class="headerlink" title="16.1.2 挂载数据卷"></a>16.1.2 挂载数据卷</h3><p>把nginx-vol数据卷挂载到/usr/share/nginx/html,挂载后容器内的文件会同步到数据卷中</p>
<ol>
<li><p>第一种挂载方式</p>
<figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ktm30qlacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-ktm30qlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs routeros">docker <span class="hljs-built_in">run</span> -d  <span class="hljs-attribute">--name</span>=nginx1 --mount <span class="hljs-attribute">src</span>=nginx-vol,dst=/usr/share/nginx/html nginx<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-22-34-47_6b9b7d72.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
</li>
<li><p>第二种挂载方式</p>
<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-odpao9lacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-odpao9lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker run -d  --name=nginx2  -v nginx-vol:<span class="hljs-regexp">/usr/</span>share<span class="hljs-regexp">/nginx/</span>html -p <span class="hljs-number">3000</span>:<span class="hljs-number">80</span> nginx<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-22-37-01_8564296d.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
</li>
</ol>
<h3 id="16-1-3-删除数据卷"><a href="#16-1-3-删除数据卷" class="headerlink" title="16.1.3 删除数据卷"></a>16.1.3 删除数据卷</h3><figure class="highlight axapta"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-31rfb1lacl5vpi"></i><span>axapta</span><div class="collapse show" id="collapse-31rfb1lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs axapta">docker <span class="hljs-built_in">container</span> stop nginx1 停止容器<br>docker <span class="hljs-built_in">container</span> rm nginx1 删除容器<br>docker volume rm nginx-vol  删除数据库<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-22-28-42_a3a50734.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h4 id="16-1-4-列出已经孤立的数据盘"><a href="#16-1-4-列出已经孤立的数据盘" class="headerlink" title="16.1.4 列出已经孤立的数据盘"></a>16.1.4 列出已经孤立的数据盘</h4><figure class="highlight dockerfile"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-54h8s3lacl5vpi"></i><span>dockerfile</span><div class="collapse show" id="collapse-54h8s3lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile">docker <span class="hljs-keyword">volume</span><span class="language-bash"> <span class="hljs-built_in">ls</span> -f dangling=<span class="hljs-literal">true</span> <span class="hljs-comment">#列出已经孤立的数据盘</span></span><br></code></pre></td></tr></table></div></figure>
<h3 id="16-2-Bind-mounts"><a href="#16-2-Bind-mounts" class="headerlink" title="16.2 Bind mounts"></a>16.2 Bind mounts</h3><ul>
<li>  此方式与Linux系统的mount方式很相似，即是会覆盖容器内已存在的目录或文件，但并不会改变容器内原有的文件，当umount后容器内原有的文件就会还原</li>
<li>  创建容器的时候我们可以通过<code>-v</code>或<code>--volumn</code>给它指定一下数据盘</li>
<li>  <code>bind mounts</code> 可以存储在宿主机系统的任意位置</li>
<li>  如果源文件/目录不存在，不会自动创建，会抛出一个错误</li>
<li>  如果挂载目标在容器中非空目录，则该目录现有内容将被隐藏</li>
</ul>
<h4 id="16-2-1-默认数据盘"><a href="#16-2-1-默认数据盘" class="headerlink" title="16.2.1 默认数据盘"></a>16.2.1 默认数据盘</h4><ul>
<li>  -v 参数两种挂载数据方式都可以用</li>
</ul>
<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-wvedkslacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-wvedkslacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs awk">mkdir mnt<br>docker run -v <span class="hljs-regexp">/mnt:/m</span>nt -it --name logs centos bash<br>cd mnt<br>echo <span class="hljs-number">1</span> &gt; <span class="hljs-number">1</span>.txt<br><span class="hljs-keyword">exit</span><br>cd mnt<br>ls<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-23-18-13_7c131bd8.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<figure class="highlight ebnf"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-zykgjolacl5vpi"></i><span>ebnf</span><div class="collapse show" id="collapse-zykgjolacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">docker inspect logs</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-28-23-22-57_44f55ad3.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<ul>
<li>  <code>Source</code>的值就是我们给容器指定的数据盘在主机上的位置</li>
<li>  <code>Destination</code>的值是这个数据盘在容器上的位置</li>
</ul>
<h4 id="16-2-2-指定数据盘"><a href="#16-2-2-指定数据盘" class="headerlink" title="16.2.2 指定数据盘"></a>16.2.2 指定数据盘</h4><figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-llpvixlacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-llpvixlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs awk">mkdir ~/data<br>docker run -v ~<span class="hljs-regexp">/data:/m</span>nt -it --name logs2 centos bash<br>cd /mnt<br>echo <span class="hljs-number">3</span> &gt; <span class="hljs-number">3</span>.txt<br><span class="hljs-keyword">exit</span><br>cat ~<span class="hljs-regexp">/data/</span><span class="hljs-number">3</span>.txt<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-29-08-07-27_02399960.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-29-08-07-27"></p>
<ul>
<li>  ~/data:/mnt 把当前用户目录中的<code>data</code>目录映射到<code>/mnt</code>上</li>
</ul>
<h4 id="16-2-3-继承容器数据盘"><a href="#16-2-3-继承容器数据盘" class="headerlink" title="16.2.3 继承容器数据盘"></a>16.2.3 继承容器数据盘</h4><ul>
<li>  docker create [OPTIONS] IMAGE [COMMAND] [ARG…] 创建一个新的容器但不启动</li>
<li>  docker run –volumes-from [containerName] [OPTIONS] IMAGE [COMMAND] [ARG…]</li>
</ul>
<figure class="highlight jboss-cli"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-5jtrjklacl5vpi"></i><span>jboss-cli</span><div class="collapse show" id="collapse-5jtrjklacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs jboss-cli">docker create -v <span class="hljs-string">/mnt</span>:<span class="hljs-string">/mnt</span> <span class="hljs-params">--name</span> logger centos<br>docker run <span class="hljs-params">--volumes-from</span> logger <span class="hljs-params">--name</span> logger3 -i -t centos bash <span class="hljs-string">//</span>继承logger这个容器的数据盘映射<br><span class="hljs-keyword">cd</span> <span class="hljs-string">/mnt</span> <br>touch logger3<br>docker run <span class="hljs-params">--volumes-from</span> logger <span class="hljs-params">--name</span> logger4 -i -t centos bash<br><span class="hljs-keyword">cd</span> <span class="hljs-string">/mnt</span><br>touch logger4<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-29-08-16-15_776b5d7b.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-29-08-16-15"><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-29-08-16-31_c8a46d71.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-29-08-16-31"></p>
<h2 id="17-网络"><a href="#17-网络" class="headerlink" title="17. 网络 "></a>17. 网络 </h2><ul>
<li><p>安装Docker时，它会自动创建三个网络，bridge（创建容器默认连接到此网络）、 none 、host</p>
<ul>
<li>  None：该模式关闭了容器的网络功能,对外界完全隔离</li>
<li>  host：容器将不会虚拟出自己的网卡，配置自己的IP等，而是使用宿主机的IP和端口。</li>
<li>  bridge: 桥接网络，此模式会为每一个容器分配IP</li>
</ul>
</li>
<li><p>  可以使用该<code>--network</code>标志来指定容器应连接到哪些网络</p>
<figure class="highlight bash"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-iin8avlacl5vpi"></i><span>bash</span><div class="collapse show" id="collapse-iin8avlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker network <span class="hljs-built_in">ls</span> <span class="hljs-comment">#列出当前的网络</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-29-08-32-08_7d0b4f5c.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-29-08-32-08"></p>
<h3 id="17-1-bridge-桥接"><a href="#17-1-bridge-桥接" class="headerlink" title="17.1 bridge(桥接)"></a>17.1 bridge(桥接)</h3></li>
<li><p>  bridge网络代表所有Docker安装中存在的网络</p>
</li>
<li><p>  除非你使用该<code>docker run --network=&lt;NETWORK&gt;</code>选项指定，否则Docker守护程序默认将容器连接到此网络</p>
</li>
<li><p>  bridge模式使用 <code>--net=bridge</code> 指定，默认设置</p>
</li>
</ul>
<figure class="highlight applescript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-25eshklacl5vpi"></i><span>applescript</span><div class="collapse show" id="collapse-25eshklacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs applescript"><br>docker <span class="hljs-built_in">run</span> -d <span class="hljs-comment">--name nginx1 nginx</span><br>docker inspect bridge <span class="hljs-comment">#查看当前的桥连网络</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-00-35-30_aa60e28b.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-00-35-30"></p>
<figure class="highlight cmake"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-0tkpc5lacl5vpi"></i><span>cmake</span><div class="collapse show" id="collapse-0tkpc5lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs cmake">docker run -d --name nginx2 --link nginx1 nginx<br>docker exec -it nginx2 bash<br>apt update<br>apt <span class="hljs-keyword">install</span> -y inetutils-ping  <span class="hljs-comment">#ping</span><br>apt <span class="hljs-keyword">install</span> -y dnsutils        <span class="hljs-comment">#nslookup</span><br>apt <span class="hljs-keyword">install</span> -y net-tools       <span class="hljs-comment">#ifconfig</span><br>apt <span class="hljs-keyword">install</span> -y iproute2        <span class="hljs-comment">#ip</span><br>apt <span class="hljs-keyword">install</span> -y curl            <span class="hljs-comment">#curl</span><br>cat /etc/hosts<br>ping nginx1<br>docker inspect bridge<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-00-42-29_81ec0a94.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-00-42-29"><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-00-43-08_5eab93e5.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-00-43-08"></p>
<h3 id="17-2-none"><a href="#17-2-none" class="headerlink" title="17.2 none"></a>17.2 none</h3><ul>
<li>  none模式使用<code>--net=none</code>指定</li>
<li>  该模式关闭了容器的网络功能,对外界完全隔离</li>
</ul>
<figure class="highlight css"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-vac9rqlacl5vpi"></i><span>css</span><div class="collapse show" id="collapse-vac9rqlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs css"># <span class="hljs-attr">--net</span> 指定无网络<br>docker run -d <span class="hljs-attr">--name</span> nginx_none <span class="hljs-attr">--net</span> <span class="hljs-attribute">none</span> nginx<br>docker inspect <span class="hljs-attribute">none</span><br>docker exec -it nginx_none bash<br>ip addr<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-00-46-27_aded3c0b.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-00-46-27"></p>
<h3 id="17-3-host"><a href="#17-3-host" class="headerlink" title="17.3 host"></a>17.3 host</h3><ul>
<li>  host模式使用 <code>--net=host</code> 指定</li>
<li>  容器将不会虚拟出自己的网卡，配置自己的IP等，而是使用宿主机的IP和端口(因此也容易造成和宿主机的端口冲突，导致容器启动失败)</li>
</ul>
<figure class="highlight applescript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-5spvohlacl5vpi"></i><span>applescript</span><div class="collapse show" id="collapse-5spvohlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs applescript">docker <span class="hljs-built_in">run</span> -d <span class="hljs-comment">--name nginx_host --net host nginx</span><br>docker inspect host<br>docker exec -<span class="hljs-keyword">it</span> nginx_host bash<br>ip addr<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-00-45-16_a506954f.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-00-45-16"></p>
<h3 id="17-4-端口映射"><a href="#17-4-端口映射" class="headerlink" title="17.4 端口映射"></a>17.4 端口映射</h3><figure class="highlight mipsasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-60larrlacl5vpi"></i><span>mipsasm</span><div class="collapse show" id="collapse-60larrlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs mipsasm"><span class="hljs-comment"># 查看镜像里暴露出的端口号</span><br>docker image <span class="hljs-keyword">inspect </span>nginx<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-01-18-01_92aa8c7b.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-18-01"></p>
<figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-942wm8lacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-942wm8lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs routeros"><span class="hljs-comment"># 让宿主机的8080端口映射到docker容器的80端口</span><br>docker <span class="hljs-built_in">run</span> -d --name port_nginx -p 8080:80  nginx<br><span class="hljs-comment"># 查看主机绑定的端口</span><br>docker container<span class="hljs-built_in"> port </span>port_nginx<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/linux/2022-10-30-01-24-02_b42c808c.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-24-02"></p>
<h3 id="17-5-指向主机的随机端口"><a href="#17-5-指向主机的随机端口" class="headerlink" title="17.5 指向主机的随机端口"></a>17.5 指向主机的随机端口</h3><figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-o06rk3lacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-o06rk3lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs routeros">docker <span class="hljs-built_in">run</span> -d --name random_nginx --publish 80 nginx<br>docker<span class="hljs-built_in"> port </span>random_nginx<br><span class="hljs-comment">#或者可以使用以下两种</span><br>docker <span class="hljs-built_in">run</span> -d --name randomall_nginx --publish-all nginx<br>docker <span class="hljs-built_in">run</span> -d --name randomall_nginx --P nginx<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/linux/2022-10-30-01-25-47_bbd93953.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-25-47"></p>
<h3 id="17-6-创建自定义网络"><a href="#17-6-创建自定义网络" class="headerlink" title="17.6 创建自定义网络"></a>17.6 创建自定义网络</h3><ul>
<li>  可以创建多个网络，每个网络IP范围均不相同</li>
<li>  docker的自定义网络里面有一个DNS服务，可以通过容器名称访问主机</li>
</ul>
<figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-1zput6lacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-1zput6lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs routeros"><span class="hljs-comment"># 创建自定义网络</span><br>docker<span class="hljs-built_in"> network </span>create --driver<span class="hljs-built_in"> bridge </span>myweb<br><span class="hljs-comment"># 查看自定义网络中的主机</span><br>docker<span class="hljs-built_in"> network </span>inspect myweb<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/linux/2022-10-30-01-29-38_3bd028cc.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-29-38"><br>s</p>
<figure class="highlight applescript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-0qnn3alacl5vpi"></i><span>applescript</span><div class="collapse show" id="collapse-0qnn3alacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs applescript"><span class="hljs-comment"># 创建容器的时候指定网络</span><br>docker <span class="hljs-built_in">run</span> -d <span class="hljs-comment">--name mynginx1  --net myweb nginx</span><br>docker <span class="hljs-built_in">run</span> -d <span class="hljs-comment">--name mynginx2  --net myweb nginx</span><br>docker exec -<span class="hljs-keyword">it</span> mynginx2 bash<br>ping mynginx1<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-01-34-42_c681320b.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-34-42"></p>
<h3 id="17-7-连接到指定网络"><a href="#17-7-连接到指定网络" class="headerlink" title="17.7 连接到指定网络"></a>17.7 连接到指定网络</h3><figure class="highlight routeros"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-kss18alacl5vpi"></i><span>routeros</span><div class="collapse show" id="collapse-kss18alacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs routeros">docker <span class="hljs-built_in">run</span> -d --name mynginx3   nginx<br>docker<span class="hljs-built_in"> network </span>connect  myweb mynginx3 #连接到myweb局域网中<br>docker<span class="hljs-built_in"> network </span>inspect myweb | grep -A 25 Containers<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-01-36-08_344779ef.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-36-08"></p>
<figure class="highlight mipsasm"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-fw7st1lacl5vpi"></i><span>mipsasm</span><div class="collapse show" id="collapse-fw7st1lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs mipsasm">docker network <span class="hljs-keyword">disconnect </span>myweb mynginx3  <span class="hljs-comment">#断开链接myweb局域网</span><br>docker network <span class="hljs-keyword">inspect </span>myweb <span class="hljs-title">| grep -A 20 Containers</span><br></code></pre></td></tr></table></div></figure>



<h3 id="17-8-移除网络"><a href="#17-8-移除网络" class="headerlink" title="17.8 移除网络"></a>17.8 移除网络</h3><figure class="highlight bash"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-b41fhglacl5vpi"></i><span>bash</span><div class="collapse show" id="collapse-b41fhglacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker network <span class="hljs-built_in">rm</span> myweb<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-01-38-52_95ad4238.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-38-52"></p>
<h2 id="18-compose"><a href="#18-compose" class="headerlink" title="18.compose "></a>18.compose </h2><ul>
<li>  Compose 通过一个配置文件来管理多个Docker容器</li>
<li>  在配置文件中，所有的容器通过services来定义，然后使用docker-compose脚本来启动、停止和重启应用和应用中的服务以及所有依赖服务的容器</li>
<li>步骤：<ul>
<li>  最后，运行<code>docker-compose up</code>，Compose 将启动并运行整个应用程序 配置文件组成</li>
<li>  services 可以定义需要的服务，每个服务都有自己的名字、使用的镜像、挂载的数据卷所属的网络和依赖的其它服务</li>
<li>  networks 是应用的网络，在它下面可以定义使用的网络名称，类性</li>
<li>  volumes是数据卷，可以在此定义数据卷，然后挂载到不同的服务上面使用</li>
</ul>
</li>
</ul>
<h3 id="18-1-安装compose"><a href="#18-1-安装compose" class="headerlink" title="18.1 安装compose"></a>18.1 安装compose</h3><ul>
<li>第一种安装方法<figure class="highlight cmake"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-k77g48lacl5vpi"></i><span>cmake</span><div class="collapse show" id="collapse-k77g48lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs cmake">yum -y <span class="hljs-keyword">install</span> epel-release<br>yum -y <span class="hljs-keyword">install</span> python-pip<br>yum clean all<br>pip <span class="hljs-keyword">install</span> docker-compose<br></code></pre></td></tr></table></div></figure></li>
<li>第二种安装方法<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-nu1xbnlacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-nu1xbnlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs awk">sudo curl -L <span class="hljs-string">&quot;https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)&quot;</span> -o <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/bin/</span>docker-compose<br><span class="hljs-comment"># 添加可执行权限</span><br>sudo chmod +x <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/bin/</span>docker-compose<br><span class="hljs-comment"># 查看版本信息 </span><br>docker-compose -versio<br></code></pre></td></tr></table></div></figure></li>
</ul>
<h3 id="18-2-编写docker-compose-yml"><a href="#18-2-编写docker-compose-yml" class="headerlink" title="18.2 编写docker-compose.yml"></a>18.2 编写docker-compose.yml</h3><ul>
<li>  在 <code>docker-compose.yml</code> 中定义组成应用程序的服务，以便它们可以在隔离的环境中一起运行</li>
<li>  空格缩进表示层次</li>
<li>  冒号空格后面有空格</li>
</ul>
<p>docker-compose.yml</p>
<figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-taq9pylacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-taq9pylacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-symbol">version:</span> <span class="hljs-string">&#x27;2&#x27;</span><br><span class="hljs-symbol">services:</span><br><span class="hljs-symbol">  nginx1:</span><br><span class="hljs-symbol">    image:</span> nginx<br><span class="hljs-symbol">    ports:</span><br>      - <span class="hljs-string">&quot;8080:80&quot;</span><br><span class="hljs-symbol">  nginx2:</span><br><span class="hljs-symbol">    image:</span> nginx<br><span class="hljs-symbol">    ports:</span><br>      - <span class="hljs-string">&quot;8081:80&quot;</span><br></code></pre></td></tr></table></div></figure>

<h3 id="18-3-启动服务"><a href="#18-3-启动服务" class="headerlink" title="18.3 启动服务"></a>18.3 启动服务</h3><ul>
<li>  docker会创建默认的网络</li>
</ul>
<table>
<thead>
<tr>
<th>命令</th>
<th>服务</th>
</tr>
</thead>
<tbody><tr>
<td>docker-compose up</td>
<td>启动所有的服务</td>
</tr>
<tr>
<td>docker-compose up -d</td>
<td>后台启动所有的服务</td>
</tr>
<tr>
<td>docker-compose ps</td>
<td>打印所有的容器</td>
</tr>
<tr>
<td>docker-compose stop</td>
<td>停止所有服务</td>
</tr>
<tr>
<td>docker-compose logs -f</td>
<td>持续跟踪日志</td>
</tr>
<tr>
<td>docker-compose exec nginx1 bash</td>
<td>进入nginx1服务系统</td>
</tr>
<tr>
<td>docker-compose rm nginx1</td>
<td>删除服务容器</td>
</tr>
<tr>
<td>docker network ls</td>
<td>查看网络网络不会删除</td>
</tr>
<tr>
<td>docker-compose down</td>
<td>删除所有的网络和容器</td>
</tr>
</tbody></table>
<h3 id="18-4-网络互ping"><a href="#18-4-网络互ping" class="headerlink" title="18.4 网络互ping"></a>18.4 网络互ping</h3><figure class="highlight autoit"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ydh5yrlacl5vpi"></i><span>autoit</span><div class="collapse show" id="collapse-ydh5yrlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs autoit">docker-compose up -d<br>docker-compose exec nginx1 bash<br>apt update &amp;&amp; apt install -y inetutils-<span class="hljs-built_in">ping</span><br><span class="hljs-meta">#可以通过服务的名字连接到对方</span><br><span class="hljs-built_in">ping</span> nginx2 <br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-01-51-45_7c0d4f4f.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-01-51-45"></p>
<h3 id="18-5-配置数据卷"><a href="#18-5-配置数据卷" class="headerlink" title="18.5 配置数据卷"></a>18.5 配置数据卷</h3><ul>
<li>  networks 指定自定义网络</li>
<li>  volumes 指定数据卷</li>
<li>  数据卷在宿主机的位置 <code>/var/lib/docker/volumes/nginx-compose_data/_data</code></li>
</ul>
<figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-4evv3clacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-4evv3clacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-symbol">version:</span> <span class="hljs-string">&#x27;2&#x27;</span><br><span class="hljs-symbol">services:</span><br><span class="hljs-symbol">  nginx1:</span>  <span class="hljs-meta">#容器名</span><br><span class="hljs-symbol">    image:</span> nginx <span class="hljs-meta">#镜像</span><br><span class="hljs-symbol">    ports:</span>       <span class="hljs-meta">#端口映射</span><br>      - <span class="hljs-string">&quot;8081:80&quot;</span><br><span class="hljs-symbol">    networks:</span><br>      - <span class="hljs-string">&quot;myweb&quot;</span> <span class="hljs-meta">#挂载到myweb网络中</span><br><span class="hljs-symbol">    volumes:</span><br>      - <span class="hljs-string">&quot;data:/data&quot;</span> <span class="hljs-meta">#相当于-v  也就是数据卷名称/容器中的路径</span><br>      - <span class="hljs-string">&quot;./nginx1:/usr/share/nginx/html&quot;</span>  <span class="hljs-meta"># 相对路径中的宿主机nginx1文件夹映射到容器的/usr/share/nginx/html文件夹</span><br><span class="hljs-symbol">  nginx2:</span><br><span class="hljs-symbol">    image:</span> nginx<br><span class="hljs-symbol">    ports:</span><br>      - <span class="hljs-string">&quot;8082:80&quot;</span><br><span class="hljs-symbol">    networks:</span><br>      - <span class="hljs-string">&quot;myweb&quot;</span> <span class="hljs-meta">#挂载到myweb网络中</span><br><span class="hljs-symbol">    volumes:</span><br>      - <span class="hljs-string">&quot;data:/data&quot;</span> <span class="hljs-meta">#相当于-v  也就是数据卷名称/容器中的路径</span><br>      - <span class="hljs-string">&quot;./nginx2:/usr/share/nginx/html&quot;</span> <br><span class="hljs-symbol">  networks:</span> <span class="hljs-meta">#网络</span><br><span class="hljs-symbol">    myweb:</span> <span class="hljs-meta">#网络名称</span><br><span class="hljs-symbol">      driver:</span> bridge <span class="hljs-meta">#网络方式</span><br><span class="hljs-symbol">  volumes:</span> <span class="hljs-meta">#数据卷</span><br><span class="hljs-symbol">    data:</span> <span class="hljs-meta">#数据卷名称</span><br><span class="hljs-symbol">      driver:</span> local <span class="hljs-meta">#数据卷驱动，就是本地硬盘上</span><br></code></pre></td></tr></table></div></figure>

<figure class="highlight awk"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-46en8vlacl5vpi"></i><span>awk</span><div class="collapse show" id="collapse-46en8vlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs awk">docker exec -it compose_nginx1_1 bash<br>cd /data <br>touch <span class="hljs-number">1</span>.txt<br><span class="hljs-keyword">exit</span><br>cd <span class="hljs-regexp">/var/</span>lib<span class="hljs-regexp">/docker/</span>volumes<span class="hljs-regexp">/compose_data/</span>_data<br>ls<br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-02-10-09_49133862.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-02-10-09"></p>
<h2 id="19-node项目"><a href="#19-node项目" class="headerlink" title="19. node项目 "></a>19. node项目 </h2><ul>
<li>  nodeapp 是一个用 Docker 搭建的本地 Node.js 应用开发与运行环境。</li>
</ul>
<h3 id="19-1-服务分类"><a href="#19-1-服务分类" class="headerlink" title="19.1 服务分类"></a>19.1 服务分类</h3><ul>
<li>  db：使用 <code>mariadb</code> 作为应用的数据库</li>
<li>  node：启动<code>node</code>服务</li>
<li>  web：使用 <code>nginx</code> 作为应用的 web 服务器</li>
</ul>
<h3 id="19-2-app目录结构"><a href="#19-2-app目录结构" class="headerlink" title="19.2 app目录结构"></a>19.2 app目录结构</h3><table>
<thead>
<tr>
<th>文件</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>docker-compose.yml</td>
<td>定义本地开发环境需要的服务</td>
</tr>
<tr>
<td>images/nginx/config/default.conf</td>
<td>nginx 配置文件</td>
</tr>
<tr>
<td>images/node/Dockerfile</td>
<td>node的Dockfile配置文件</td>
</tr>
<tr>
<td>images/node/web/package.json</td>
<td>项目文件</td>
</tr>
<tr>
<td>images/node/web/public/index.html</td>
<td>静态首页</td>
</tr>
<tr>
<td>images/node/web/server.js</td>
<td>node服务</td>
</tr>
</tbody></table>
<figure class="highlight pgsql"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-cl16a5lacl5vpi"></i><span>pgsql</span><div class="collapse show" id="collapse-cl16a5lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs pgsql">├── docker-compose.yml<br>└── images<br>    ├── nginx<br>    │   └── config<br>    │       └── <span class="hljs-keyword">default</span>.conf<br>    └── node<br>        ├── Dockerfile<br>        └── web<br>            ├── package.json<br>            ├── <span class="hljs-built_in">public</span><br>            │   └── <span class="hljs-keyword">index</span>.html<br>            └── <span class="hljs-keyword">server</span>.js<br><br></code></pre></td></tr></table></div></figure>

<h4 id="19-2-1-docker-compose-yml"><a href="#19-2-1-docker-compose-yml" class="headerlink" title="19.2.1 docker-compose.yml"></a>19.2.1 docker-compose.yml</h4><figure class="highlight dts"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-w4btrhlacl5vpi"></i><span>dts</span><div class="collapse show" id="collapse-w4btrhlacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs dts"><span class="hljs-symbol">version:</span> <span class="hljs-string">&#x27;2&#x27;</span><br><span class="hljs-symbol">services:</span><br><span class="hljs-symbol">  node:</span> <span class="hljs-meta">#容器名</span><br><span class="hljs-symbol">    build:</span>  <span class="hljs-meta">#node镜像需要构建</span><br><span class="hljs-symbol">      context:</span> .<span class="hljs-keyword">/images/</span>node <span class="hljs-meta">#需要Dockerfile文件在./images/node下查找</span><br><span class="hljs-symbol">      dockerfile:</span> Dockerfile<br><span class="hljs-symbol">    depends_on:</span> <span class="hljs-meta">#依赖</span><br>      - db <span class="hljs-meta">#需要等db容器启动后再启动node容器</span><br><span class="hljs-symbol">  web:</span><br><span class="hljs-symbol">    image:</span> nginx<br><span class="hljs-symbol">    ports:</span><br>      - <span class="hljs-string">&quot;8088:80&quot;</span><br><span class="hljs-symbol">    volumes:</span><br>      - .<span class="hljs-keyword">/images/</span>nginx/config:<span class="hljs-keyword">/etc/</span>nginx/conf.d<br>      - .<span class="hljs-keyword">/images/</span>node<span class="hljs-keyword">/web/</span>public:/public<br><span class="hljs-symbol">    depends_on:</span><br>      - node<br><span class="hljs-symbol">  db:</span><br><span class="hljs-symbol">    image:</span> mariadb<br><span class="hljs-symbol">    environment:</span> <span class="hljs-meta">#环境变量</span><br><span class="hljs-symbol">      MYSQL_ROOT_PASSWORD:</span> <span class="hljs-string">&quot;root&quot;</span><br><span class="hljs-symbol">      MYSQL_DATABASE:</span> <span class="hljs-string">&quot;node&quot;</span><br><span class="hljs-symbol">      MYSQL_USER:</span> <span class="hljs-string">&quot;zhufeng&quot;</span><br><span class="hljs-symbol">      MYSQL_PASSWORD:</span> <span class="hljs-string">&quot;123456&quot;</span><br><span class="hljs-symbol">    volumes:</span><br>      - data:<span class="hljs-keyword">/var/</span>lib/mysql<br><span class="hljs-symbol">volumes:</span><br><span class="hljs-symbol">  data:</span><br><span class="hljs-symbol">    driver:</span> local<br></code></pre></td></tr></table></div></figure>

<h4 id="19-2-2-server-js"><a href="#19-2-2-server-js" class="headerlink" title="19.2.2 server.js"></a>19.2.2 server.js</h4><p>images/node/web/server.js</p>
<figure class="highlight pgsql"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-0mgzlclacl5vpi"></i><span>pgsql</span><div class="collapse show" id="collapse-0mgzlclacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs pgsql">let http=require(<span class="hljs-string">&#x27;http&#x27;</span>);<br>var mysql  = require(<span class="hljs-string">&#x27;mysql&#x27;</span>);<br>var <span class="hljs-keyword">connection</span> = mysql.createConnection(&#123;<br>  host     : <span class="hljs-string">&#x27;db&#x27;</span>,<br>  <span class="hljs-keyword">user</span>     : <span class="hljs-string">&#x27;zfpx&#x27;</span>,<br>  <span class="hljs-keyword">password</span> : <span class="hljs-string">&#x27;123456&#x27;</span>,<br>  <span class="hljs-keyword">database</span> : <span class="hljs-string">&#x27;node&#x27;</span><br>&#125;);<br><br><span class="hljs-keyword">connection</span>.<span class="hljs-keyword">connect</span>();<br><br>let <span class="hljs-keyword">server</span>=http.createServer(<span class="hljs-keyword">function</span> (req,res) &#123;<br>    <span class="hljs-keyword">connection</span>.query(<span class="hljs-string">&#x27;SELECT 2 + 2 AS solution&#x27;</span>, <span class="hljs-keyword">function</span> (error, results, fields) &#123;<br>        <span class="hljs-keyword">if</span> (error) throw error;<br>        res.<span class="hljs-keyword">end</span>(<span class="hljs-string">&#x27;&#x27;</span>+results[<span class="hljs-number">0</span>].solution);<br>    &#125;);<br>&#125;);<br><span class="hljs-keyword">server</span>.<span class="hljs-keyword">listen</span>(<span class="hljs-number">3000</span>);<br><br></code></pre></td></tr></table></div></figure>

<h4 id="19-2-3-package-json"><a href="#19-2-3-package-json" class="headerlink" title="19.2.3 package.json"></a>19.2.3 package.json</h4><p>images/node/web/package.json</p>
<figure class="highlight json"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-neyznflacl5vpi"></i><span>json</span><div class="collapse show" id="collapse-neyznflacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br><span class="hljs-attr">&quot;scripts&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;start&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;node server.js&quot;</span><br>  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br><span class="hljs-attr">&quot;dependencies&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;mysql&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;^2.16.0&quot;</span><br><span class="hljs-punctuation">&#125;</span><br><br></code></pre></td></tr></table></div></figure>

<h4 id="19-2-4-images-node-Dockerfile"><a href="#19-2-4-images-node-Dockerfile" class="headerlink" title="19.2.4 images/node/Dockerfile"></a>19.2.4 images/node/Dockerfile</h4><figure class="highlight dockerfile"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-dqqix8lacl5vpi"></i><span>dockerfile</span><div class="collapse show" id="collapse-dqqix8lacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile"><span class="hljs-keyword">FROM</span> node<br><span class="hljs-keyword">MAINTAINER</span> zhangrenyang &lt;zhang_renyang@<span class="hljs-number">126</span>.com&gt;<br><span class="hljs-keyword">COPY</span><span class="language-bash"> ./web /web</span><br><span class="hljs-keyword">WORKDIR</span><span class="language-bash"> /web</span><br><span class="hljs-keyword">RUN</span><span class="language-bash"> npm install</span><br><span class="hljs-keyword">CMD</span><span class="language-bash"> npm start</span><br><br></code></pre></td></tr></table></div></figure>

<h4 id="19-2-5-images-nginx-config-default-conf"><a href="#19-2-5-images-nginx-config-default-conf" class="headerlink" title="19.2.5 images/nginx/config/default.conf"></a>19.2.5 images/nginx/config/default.conf</h4><figure class="highlight nginx"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-9fsosllacl5vpi"></i><span>nginx</span><div class="collapse show" id="collapse-9fsosllacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs nginx"><span class="hljs-section">upstream</span> backend &#123;<br>    <span class="hljs-attribute">server</span> node:<span class="hljs-number">3000</span>;<br>&#125;<br><span class="hljs-section">server</span> &#123;<br>    <span class="hljs-attribute">listen</span> <span class="hljs-number">80</span>;<br>    <span class="hljs-attribute">server_name</span> localhost;<br>    <span class="hljs-attribute">root</span> /public;<br>    <span class="hljs-attribute">index</span> index.html index.htm;<br><br>    <span class="hljs-section">location</span> /api &#123;<br>        <span class="hljs-attribute">proxy_pass</span> http://backend;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></div></figure>

<h3 id="19-3-启动容器"><a href="#19-3-启动容器" class="headerlink" title="19.3 启动容器"></a>19.3 启动容器</h3><figure class="highlight vim"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-6uafmflacl5vpi"></i><span>vim</span><div class="collapse show" id="collapse-6uafmflacl5vpi"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs vim">docker compose <span class="hljs-keyword">up</span>  -d <br>docker <span class="hljs-keyword">ps</span> -<span class="hljs-keyword">a</span> <br>curl http://localhos<span class="hljs-variable">t:8088</span><br></code></pre></td></tr></table></div></figure>
<p><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/dev_ops/docker/2022-10-30-02-20-40_5dbb9ebc.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="2022-10-30-02-20-40"></p>
<h2 id="20-参考"><a href="#20-参考" class="headerlink" title="20. 参考 "></a>20. 参考 </h2><ul>
<li>  <a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/2016/07/yaml.html">yaml</a></li>
<li>  <a target="_blank" rel="noopener" href="https://www.npmjs.com/package/mysql">mysql</a></li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%BF%90%E7%BB%B4/" class="category-chain-item">运维</a>
  
  
    <span>></span>
    
  <a href="/categories/%E8%BF%90%E7%BB%B4/docker/" class="category-chain-item">docker</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%BF%90%E7%BB%B4/">#运维</a>
      
        <a href="/tags/docker/">#docker</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>01.docker基础</div>
      <div>http://81.68.132.127/2022/10/24/dev_ops/docker/01.docker基础/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Ikki</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月24日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2022年10月30日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/10/27/dev_ops/linux/10.%E7%BD%91%E7%BB%9C/" title="10.linux网络">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">10.linux网络</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/10/24/dev_ops/linux/crontab%E7%B3%BB%E7%BB%9F%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" title="08.crontab定时任务">
                        <span class="hidden-mobile">08.crontab定时任务</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  <article id="comments" lazyload>
    
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"6KzYWc2oaEBaHgNl4UIicMJC-gzGzoHsz","appKey":"vJ4FIwKeHRmHraCJraAsjyZ7","path":"window.location.pathname","placeholder":"请输入你的评论\n昵称为必填项目(输入QQ号码可以自动获取昵称和邮箱)\n虽然email不是必选，但是填写了email可以收到推送通知哦！","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":true,"recordIP":true,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":true},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


  </article>


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      IKKI-专业BUG开发
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/fluid/js/star.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
